package org.zk.dubbo_provider.service.impl;



import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.zk.dubbo_provider.dao.*;
import org.zk.dubbo_provider.utils.AdminBillUtil;
import org.zk.entity.*;
import org.zk.service.AdminBillService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Alicelonely
 * @since 2019-12-11
 */
@Service(timeout = 10000)
public class AdminBillServiceImpl implements AdminBillService {

    @Resource
    private AdminBillMapper adminBillMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private AdminBillUtil adminBillUtil;
    @Resource
    private TeacherBillMapper teacherBillMapper;
    @Resource
    private TeacherMapper teacherMapper;


    /**
     * 姚家乐
     * 2019-12-15 11：28
     * 查看老师财务部分信息
     * @return
     */

    @Override
    public PageInfo<Teacher> getTeacherBillPage(Map<String,Object> map) {
        PageInfo<Teacher> pageInfo;

        int pageSize=Integer.parseInt(String.valueOf(map.get("pageSize")));
        int pageNum=Integer.parseInt(String.valueOf(map.get("pageNum")));
        PageHelper.startPage(pageNum,pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            List<Teacher> list=teacherMapper.getTeacherBill((String) map.get("seetext"));
            //System.out.println("分页数据："+List);
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
            pageInfo = new PageInfo<>(list,pageSize);

        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }

        return pageInfo;
    }

    /**
     * 姚家乐
     * 2019-12-15 5:53
     * 通过教师ID查看并计算
     * @param teacherid
     * @return
     */
    @Override
    public Map<String, Object> getCountOrderByTeacherid(int teacherid) {
        List<Order> orderList = null;
        Double teahcerprice = 0.00;
        try {
            orderList = orderMapper.getCountOrderByTeacherid(teacherid);//根据老师ID获得该老师的课程订单
            teahcerprice = teacherBillMapper.getTeacherbillpriceByTeacherid(teacherid);//根据老师ID获得该老师的历史总收益


        }catch (Exception e){
            e.printStackTrace();
        }
        return adminBillUtil.acCount(orderList,teahcerprice);//返回对其在工具包中进行的计算处理
    }

    /**
     * 姚家乐
     * 2019-12-18 11:34
     * 向教师账单表中插入条件月需发工资的数据信息
     * @return
     */
    @Override
    public Boolean insertTeacherBill() {
        Boolean t = false;
        if (teacherBillMapper.getTeacherBillByTime()>0){
            return  t;
        }else {
            List<AdminBill> list = adminBillMapper.getAllPayoffToTeacher();
            int n = list.size();//得到需要发工资的老师数量
            if (n>0){
                for (int i = 0; i < n ; i++) {
                    Double price = list.get(i).getAdminBillPrice();
                    BigDecimal b1 = BigDecimal.valueOf(price);
                    BigDecimal b2 = BigDecimal.valueOf(0.90);
                    if (price <= 1000.00){
                        price = b2.multiply(b1).doubleValue();
                    }else if (price<= 3000.00){
                        BigDecimal b3 = BigDecimal.valueOf(1000.00);
                        BigDecimal b4 = BigDecimal.valueOf(0.85);
                        price = b3.multiply(b2).add(b1.subtract(b3).multiply(b4)).doubleValue();
                    }else if (price<= 6000.00){
                        BigDecimal b3 = BigDecimal.valueOf(1000.00);
                        BigDecimal b4 = BigDecimal.valueOf(2000.00);
                        BigDecimal b5 = BigDecimal.valueOf(3000.00);
                        BigDecimal b6 = BigDecimal.valueOf(0.85);
                        BigDecimal b7 = BigDecimal.valueOf(0.80);
                        price = b3.multiply(b2).add(b4.multiply(b6)).add(b1.subtract(b5).multiply(b7)).doubleValue();
                        /*list.get(i).setAdminBillPrice(1000.00*0.9+2000*0.85 + (price-3000.00) *0.8);*/
                    }else if (price > 6000.00){
                        BigDecimal b3 = BigDecimal.valueOf(1000.00);
                        BigDecimal b4 = BigDecimal.valueOf(2000.00);
                        BigDecimal b5 = BigDecimal.valueOf(3000.00);
                        BigDecimal b6 = BigDecimal.valueOf(6000.00);
                        BigDecimal b7 = BigDecimal.valueOf(0.85);
                        BigDecimal b8 = BigDecimal.valueOf(0.80);
                        BigDecimal b9 = BigDecimal.valueOf(0.70);
                        price = b3.multiply(b2).add(b4.multiply(b7)).add(b5.multiply(b8)).add(b1.subtract(b6).multiply(b9)).doubleValue();
                        /*list.get(i).setAdminBillPrice(1000.00*0.9+2000*0.85+3000*0.8 + (price-6000.00) *0.7);*/
                    }
                    BigDecimal b = new BigDecimal(price);
                    final double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    System.out.println(f1);
                    list.get(i).setAdminBillPrice(f1);
                }
                int m = teacherBillMapper.insertTeacherBill(list);//向教师账单表中插入需要发放工资的记录
                if (n == m){
                    t = true;
                }
            }
            return t;
        }
    }


    /**
     * 姚家乐
     * 2019-12-17 11:17
     * 遍历出未发工资的教师,以及应发工资的金额
     * 在每月10号，进行教师未发工资的生成，冰箱教师的账单中插入，并标识未处理
     * @return
     */
    @Override
    public PageInfo<TeacherBill> getPayoffToTeacherPage(Map<String, Object> map) {
        PageInfo<TeacherBill> pageInfo;
        List<TeacherBill> teacherBills;
        Date date = new Date();//得到当前的时间戳
        Calendar calendar = Calendar.getInstance();//对月份进行操作
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);

        int pageSize=Integer.parseInt(String.valueOf(map.get("pageSize")));
        int pageNum=Integer.parseInt(String.valueOf(map.get("pageNum")));
        if (day == 7){//每月10号发工资，判断是否为10号
            PageHelper.startPage(pageNum,pageSize);
            try {
                teacherBills = teacherBillMapper.getTeacherBillByflag0((String) map.get("seetext"));
                //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
                pageInfo = new PageInfo<>(teacherBills,pageSize);
            }finally {
                PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
            }
            return pageInfo;
        }else {
            PageHelper.startPage(pageNum,pageSize);
            try {
                teacherBills = new ArrayList<>();
                //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
                pageInfo = new PageInfo<>(teacherBills,pageSize);
            }finally {
                PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
            }
            return pageInfo;
        }
    }

    /**
     * 姚家乐
     * 2019-12-18 9:35
     * 给教师钱包转账后的返回值,如果工资发放成功，
     * (1)修改教师账单记录表中的发放工资的状态栏，修改为  1
     * (2)就向管理员的账单表中插入工资发放的数据
     * @return
     */
    @Override
    public Boolean updateTeacherfer(int adminid,int teacherid,Double teachermoney,Double price) {
        double money = teachermoney + price;
        Map<String,Object> map = new HashMap<>();
        map.put("money",money);
        map.put("teacherid",teacherid);
        int n = teacherMapper.getTransfer(map);
        Boolean m = false;

        if (n >= 1){
            m = true;

            /*以下是修改这次发放工资成功后的老师账单表中的状态，修改为 1 */
            teacherBillMapper.updateTeacherBillFlag(teacherid);
            /*以下是向管理员账单表中插入工资发放的数据*/
            /*Map<String,Object> map1 = new HashMap<>();
            map1.put("teacherid",teacherid);
            map1.put("price",price);
            map1.put("adminid",adminid);
            adminBillMapper.insertTeacherBillToAdminBill(map1);*/
        }
        return m;
    }

    /**
     * 姚家乐
     * 2019-12-25 3:57
     * 根据教师的ID查看教师的历史交易记录
     * @param map
     * @return
     */
    @Override
    public PageInfo<TeacherBill> getTeacherbillByTeacherid0Page(Map<String,Object> map) {
        PageInfo<TeacherBill> pageInfo;

        int pageSize=Integer.parseInt(String.valueOf(map.get("pageSize")));
        int pageNum=Integer.parseInt(String.valueOf(map.get("pageNum")));
        PageHelper.startPage(pageNum,pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            List<TeacherBill> list = teacherBillMapper.getTeacherbillByTeacherid0(map);
            System.out.println("分页数据："+list);
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
            pageInfo = new PageInfo<>(list,pageSize);

        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }


        return pageInfo;
    }


    @Override
    public PageInfo getOutPage(Map map) {
        PageInfo<AdminBill> pageInfo;

        int pageSize=Integer.parseInt(String.valueOf(map.get("pageSize")));
        int pageNum=Integer.parseInt(String.valueOf(map.get("pageNum")));
        PageHelper.startPage(pageNum,pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            List<AdminBill> list=adminBillMapper.getOutAdminBill(map);
            //System.out.println("分页数据："+List);
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
            pageInfo = new PageInfo<>(list,pageSize);

        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }

        return pageInfo;
    }

    @Override
    public PageInfo getInPage(Map map) {
        PageInfo<AdminBill> pageInfo;

        int pageSize=Integer.parseInt(String.valueOf(map.get("pageSize")));
        int pageNum=Integer.parseInt(String.valueOf(map.get("pageNum")));
        PageHelper.startPage(pageNum,pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            List<AdminBill> list=adminBillMapper.getInAdminBill(map);
            //System.out.println("分页数据："+List);
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
            pageInfo = new PageInfo<>(list,pageSize);

        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }

        return pageInfo;
    }

    @Override
    public void delOutAdminBill(int id) {
        adminBillMapper.delOutAdminBill(id);
    }

    @Override
    public void delInAdminBill(int id) {
        adminBillMapper.delInAdminBill(id);
    }

    @Override
    public void insertOutAdminBill(int id) {
        adminBillMapper.insertOutAdminBill(id);
    }



    public AdminBillMapper getAdminBillMapper() {
        return adminBillMapper;
    }

    public void setAdminBillMapper(AdminBillMapper adminBillMapper) {
        this.adminBillMapper = adminBillMapper;
    }

}
